﻿@page "/datagrid-single-selection"
@using Radzen
@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inject NorthwindContext dbContext
<h1>DataGrid <strong>Single Selection</strong></h1>

<RadzenExample Name="DataGridSingleSelection" Documentation="false" Heading="false">
    <div style="display: flex; align-items: center; margin-bottom: 16px">
    <RadzenButton Click="@ClearSelection" Text="Clear Selection" />
    @if (selectedEmployees?.Any() == true)
    {
    <div style="margin-left: 16px">
        Selected Employee: @selectedEmployees[0].FirstName @selectedEmployees[0].LastName
    </div>
    }
    </div>

    <RadzenDataGrid AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" AllowPaging="true" PageSize="4"
                AllowSorting="true" Data="@employees" TItem="Employee" ColumnWidth="200px"
                SelectionMode="DataGridSelectionMode.Single" @bind-Value=@selectedEmployees>
        <Columns>
            <RadzenDataGridColumn TItem="Employee" Property="EmployeeID" Title="Employee ID" />
            <RadzenDataGridColumn TItem="Employee" Property="Photo" Title="Employee" Sortable="false" Filterable="false">
                <Template Context="data">
                    <RadzenImage Path="@data.Photo" style="width: 40px; height: 40px; border-radius: 8px;" />
                    @data.FirstName @data.LastName
                </Template>
            </RadzenDataGridColumn>
            <RadzenDataGridColumn TItem="Employee" Property="Title" Title="Title" />
            <RadzenDataGridColumn TItem="Employee" Property="HireDate" Title="Hire Date" FormatString="{0:d}" />
            <RadzenDataGridColumn TItem="Employee" Property="City" Title="City" />
            <RadzenDataGridColumn TItem="Employee" Property="Country" Title="Country" />
        </Columns>
    </RadzenDataGrid>
</RadzenExample>
@code {
    IEnumerable<Employee> employees;
    IList<Employee> selectedEmployees;

    void ClearSelection()
    {
        selectedEmployees = null;
    }

    protected override void OnInitialized()
    {
        employees = dbContext.Employees;
        selectedEmployees = employees.Take(1).ToList();
    }
}